package com.bdqn.t313.test;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * DOM解析XML文档
 */
public class DomParseXML {

    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {
        //1、创建解析器工厂实例
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        //2、创建DocumentBuilder对象
        DocumentBuilder db = builderFactory.newDocumentBuilder();
        //3、解析XML文档，得到一个Document对象
        Document document = db.parse("XMLParse_ch02/src/main/resources/book.xml");
        //4、获取节点列表
        NodeList bookList = document.getElementsByTagName("book");
        for (int i = 0; i<bookList.getLength();i++){
            //获取单个节点
            Node book = bookList.item(i);
            //获取元素
            Element brandEle = (Element)book;
            //获取属性id
            String bookId = brandEle.getAttribute("id");
            System.out.println(bookId);
            //获取book下的title子节点
            NodeList types = brandEle.getChildNodes();
            for (int j = 0;j<types.getLength();j++){
                //不获取空格字符对象
                if(types.item(j).getNodeType()==Node.ELEMENT_NODE){
                    Element typeElement = (Element) types.item(j);
                }
            }
        }

        //获取节点元素下的文本值
        NodeList titleList = document.getElementsByTagName("title");
        Element titleElement = (Element) titleList.item(0);
        //System.out.println(titleElement.getFirstChild().getNodeValue());

        //往books节点下添加book子节点
        DomParseXMLUtil domParseXMLUtil = new DomParseXMLUtil("XMLParse_ch02/src/main/resources/book.xml");
        //实例化节点实体类对象
        DomElement domElement = new DomElement();
        domElement.setRootName("books");
        domElement.setElementName("book");
        domElement.setAttributeName("id");
        domElement.setAttributeValue("id1055");
        domElement.setChildElementName("title");
        domElement.setTextContext("Mybatis");
        domParseXMLUtil.addElement(domElement);
        //1、创建book子节点
        /*Element bookElement = document.createElement("book");
        bookElement.setAttribute("id","bk1013");
        //2、创建book下的子节点title节点
        Element titleElement2 = document.createElement("title");
        //设置title下的文本值
        titleElement2.setTextContent("HTML");
        //3、添加父子关系
        //将title子节点追加到book节点下
        bookElement.appendChild(titleElement2);
        //获取books节点
        Element booksElement = (Element) document.getElementsByTagName("books").item(0);
        //将book节点追加到books节点下
        booksElement.appendChild(bookElement);
        //将追加的节点保存到xml文档中
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        StreamResult result = new StreamResult(new FileOutputStream("XMLParse_ch02/src/main/resources/book.xml"));
        //将domSource文件写入到result路径
        transformer.transform(domSource,result);
        System.out.println("追加节点成功！！");*/
    }


}
